package com.pubinfo.passbook.common.service.contract.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pubinfo.passbook.common.entity.TContractInvoiceApply;
import com.pubinfo.passbook.common.entity.TContractInvoiceHeader;
import com.pubinfo.passbook.common.mapper.TContractInvoiceHeaderMapper;
import com.pubinfo.passbook.common.model.dto.contract.InvoiceData;
import com.pubinfo.passbook.common.model.dto.contract.TaxParam;
import com.pubinfo.passbook.common.model.vo.contract.TaxListVo;
import com.pubinfo.passbook.common.service.contract.TContractInvoiceApplyService;
import com.pubinfo.passbook.common.service.contract.TContractInvoiceHeaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author admin
 * @description 针对表【t_contract_invoice_header(发票凭证)】的数据库操作Service实现
 * @createDate 2021-12-06 14:45:14
 */
@Service
public class TContractInvoiceHeaderServiceImpl extends ServiceImpl<TContractInvoiceHeaderMapper, TContractInvoiceHeader> implements TContractInvoiceHeaderService {
  @Autowired
  TContractInvoiceApplyService applyService;
  
  @Override
  public void saveInvoice(InvoiceData invoiceData) {
    //  TContractInvoiceHeader invoice = new TContractInvoiceHeader(invoiceData);
    TContractInvoiceHeader invoiceHeader = getById(invoiceData.getInvoiceId());
    //如果已经存在，则不做任何操作。
    if (invoiceHeader == null) {
      TContractInvoiceHeader invoice = new TContractInvoiceHeader(invoiceData);
      save(invoice);
      String[] orderNumber = invoiceData.getOrderNumber();
      //回写到订单表。
      applyService.lambdaUpdate().in(orderNumber.length > 0, TContractInvoiceApply::getBssOrderNum, invoiceData.getOrderNumber()).set(TContractInvoiceApply::getInvoiceId, invoiceData.getInvoiceId()).update();
      //  applyService.lambdaQuery().
    }
    
  }
  
  /**
   * 获取合同已开发票信息
   */
  @Override
  public List<TContractInvoiceHeader> getContractInvoice(Integer contractId) {
    QueryWrapper<TContractInvoiceHeader> qw = new QueryWrapper<>();
    qw.lambda().eq(TContractInvoiceHeader::getContractId, contractId);
    return this.baseMapper.selectList(qw);
  }

    /**
     * 获取税金计提列表
     */
    @Override
    public IPage<TaxListVo> taxList(TaxParam param) {
        IPage<TaxListVo> iPage = page(param.getPage());
        return baseMapper.selectTaxList(iPage,param);
    }

}




